<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>{{number}}.{{title}}</title>
    <!-- 引入ACE CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"
        charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
        charset="utf-8"></script>
    <!-- 引入jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .container .navbar {
            width: 100%;
            height: 50px;
            background-color: #343333;
            overflow: hidden;
        }

        .container .navbar a {
            display: inline-block;
            width: 40px;
            color: #f5f5f7;
            font-size: 15px;
            font-family: "PingFang SC";
            line-height: 40px;
            text-decoration: none;
            margin-top: 5px;
            margin-left: 10px;
        }

        .container .navbar a:hover {
            font-weight: bold;
        }

        .container .part1 {
            width: 100%;
            height: 600px;
            overflow: hidden;
        }

        .container .part1 .left_desc {
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
        }

        .container .part1 .left_desc h3 {
            padding-top: 10px;
            padding-left: 10px;
        }

        .container .part1 .left_desc pre {
            padding-top: 10px;
            padding-left: 10px;
            font-size: medium;
            font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        }

        .container .part1 .right_code .ace_text-input {
            width: 50%;
            float: right;
            font-family: 'Courier New', 'Gill Sans', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
        }

        .container .part1 .right_code .ace_editor {
            height: 600px;
        }

        .container .part2 {
            width: 100%;
            overflow: hidden;
        }

        .container .part2 .result {
            width: 300px;
            float: left;
        }

        .container .part2 .btn-submit {
            width: 120px;
            height: 50px;
            font-size: large;
            float: right;
            background-color: #26bb9c;
            color: #FFF;
            border: 0px;
            margin-top: 10px;
            margin-right: 10px;
        }

        .container .part2 button:hover {
            color: green;
        }

        .container .part2 .result {
            margin-top: 15px;
            margin-left: 15px;
            font-size: large;
            font-family: 'Dejavu Sans Mono', 'Courier New';
        }

        .container .part2 .result pre {
            font-size: medium;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏， 功能不实现-->
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
            <a href="#">登录</a>
        </div>

        <!-- 左右呈现，题目描述和预设代码 -->
        <div class="part1">
            <div class="left_desc">
                <h3><span id="number">{{number}}</span> . {{title}} _ {{difficulty}}</h3>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{header_code}}</textarea></pre>
            </div>
        </div>

        <!-- 提交并且得到结果，并显示 -->
        <div class="part2">
            <div class="result"></div>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>

    </div>

    <script>
        //初始化对象
        editor = ace.edit("code");

        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");

        // 字体
        editor.setFontSize(15);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);

        // 启用提示菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        // 提交信息
        function submit() {
            //1. 收集数据，题号和代码
            var code = editor.getSession().getValue();
            var number = $(".container .part1 .left_desc h3 #number").text();

            //2. 构建json，通过ajax并向后台发起请求
            var judge_url = "/judge/" + number;
            $.ajax({
                method: 'Post',
                url: judge_url,
                dataType: 'json', // 告知后端需要的数据格式

                contentType: 'application/json; charset=utf-8', // 向后端发送的数据格式
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),

                //3. 得到后台结果，显示到前端页面中
                success: function (data) // 交互成功后执行的回调函数
                {
                    show_result(data);
                }
            });
        }

        function show_result(data) {
            var result_div = $(".container .part2 .result");

            var reason_div = $("<p>", {
                text: data.reason
            });
            reason_div.appendTo(result_div);

            if (data.status == 0) {
                var stdout_lable = $("<pre>", {
                    text: data.stdout
                });
                stdout_lable.appendTo(result_div);

                var stderr_lable = $("<pre>", {
                    text: data.stderr
                });
                stdout_lable.appendTo(result_div);
            }
            else { } // 编译失败
        }

    </script>
</body>

</html>